package com.fa4j.code.mate.controller;

import com.fa4j.code.mate.model.*;
import com.fa4j.code.mate.repository.CodeMateRepository;
import com.fa4j.code.mate.service.CodeMateService;
import com.fa4j.common.base.exception.CommonRequestError;
import com.fa4j.common.base.model.Page;
import com.fa4j.common.base.model.RemoveCmd;
import com.fa4j.common.web.annotation.ApiMethod;
import com.fa4j.common.web.model.AppType;
import com.fa4j.system.login.util.LoginUtil;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 代码元数据 Web接口
 *
 * @since 2025-09-14 11:40:42
 */
@RestController
@RequestMapping("code/mate")
@RequiredArgsConstructor
public class CodeMateController {

    private final CodeMateService codeMateService;
    private final CodeMateRepository codeMateRepository;

    @GetMapping("project/page")
    @ApiMethod(name = "代码元数据项目-分页查询", type = AppType.MANAGE)
    public Page<CodeMateProject> queryProjectPage(CodeMateProjectQuery query) {
        return codeMateRepository.queryProjectPage(query);
    }

    @GetMapping("project/{projectName}")
    @ApiMethod(name = "代码元数据项目-明细查询", type = AppType.MANAGE)
    public CodeMateProject findProjectByProjectName(@PathVariable String projectName) {
        return codeMateRepository.findProjectByProjectName(projectName)
                .orElseThrow(() -> CommonRequestError.NOT_FOUND.build("项目"));
    }


    @PostMapping("project")
    @ApiMethod(name = "代码元数据项目-新增", type = AppType.MANAGE)
    public CodeMateProject createProject(@Valid @RequestBody CodeMateProjectCreateCmd cmd) {
        return codeMateService.createProject(cmd);
    }

    @PatchMapping("project")
    @ApiMethod(name = "代码元数据项目-编辑", type = AppType.MANAGE)
    public CodeMateProject modifyProject(@Valid @RequestBody CodeMateProjectModifyCmd cmd) {
        return codeMateService.modifyProject(cmd);
    }

    @PostMapping("module")
    @ApiMethod(name = "代码元数据模块-新增", type = AppType.MANAGE)
    public CodeMateModule createModule(@Valid @RequestBody CodeMateModuleCreateCmd cmd) {
        return codeMateService.createModule(cmd);
    }

    @PatchMapping("module")
    @ApiMethod(name = "代码元数据模块-编辑", type = AppType.MANAGE)
    public CodeMateModule modifyModule(@Valid @RequestBody CodeMateModuleModifyCmd cmd) {
        return codeMateService.modifyModule(cmd);
    }

    @PostMapping("model")
    @ApiMethod(name = "代码元数据模型-新增", type = AppType.MANAGE)
    public CodeMateModel createModel(@Valid @RequestBody CodeMateModelCreateCmd cmd) {
        return codeMateService.createModel(cmd);
    }

    @PatchMapping("model")
    @ApiMethod(name = "代码元数据模型-编辑", type = AppType.MANAGE)
    public CodeMateModel modifyModel(@Valid @RequestBody CodeMateModelModifyCmd cmd) {
        return codeMateService.modifyModel(cmd);
    }

    @PostMapping("field")
    @ApiMethod(name = "代码元数据字段-新增", type = AppType.MANAGE)
    public CodeMateField createField(@Valid @RequestBody CodeMateFieldCreateCmd cmd) {
        return codeMateService.createField(cmd);
    }

    @PatchMapping("field")
    @ApiMethod(name = "代码元数据字段-编辑", type = AppType.MANAGE)
    public CodeMateField modifyField(@Valid @RequestBody CodeMateFieldModifyCmd cmd) {
        return codeMateService.modifyField(cmd);
    }

    @PostMapping("method")
    @ApiMethod(name = "代码元数据方法-新增", type = AppType.MANAGE)
    public CodeMateMethod createMethod(@Valid @RequestBody CodeMateMethodCreateCmd cmd) {
        return codeMateService.createMethod(cmd);
    }

    @PatchMapping("method")
    @ApiMethod(name = "代码元数据方法-编辑", type = AppType.MANAGE)
    public CodeMateMethod modifyMethod(@Valid @RequestBody CodeMateMethodModifyCmd cmd) {
        return codeMateService.modifyMethod(cmd);
    }

    @DeleteMapping("{id}")
    @ApiMethod(name = "代码元数据-删除", type = AppType.MANAGE)
    public CodeMate remove(@PathVariable Integer id) {
        return codeMateService.remove(RemoveCmd.of(id, LoginUtil.getAccountId()));
    }

}
